package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.iwomy.secureplat.platform.dto.BasicPlaceFloorDTO;
import com.iwomy.secureplat.platform.entity.*;
import com.iwomy.secureplat.platform.utils.RemoteDeptUtils;
import com.iwomy.secureplat.platform.mapper.BasicPlaceFloorMapper;
import com.iwomy.secureplat.platform.service.BasicInstallLocationService;
import com.iwomy.secureplat.platform.service.BasicPlaceFloorService;
import com.iwomy.secureplat.platform.vo.BasicPlaceFloorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 楼层管理
 *
 * @author pig
 * @date 2023-11-30 13:55:10
 */
@Service
public class BasicPlaceFloorServiceImpl extends ServiceImpl<BasicPlaceFloorMapper, BasicPlaceFloorEntity> implements BasicPlaceFloorService {

	@Autowired
	private BasicInstallLocationService basicInstallLocationService;

	@Autowired
	private RemoteDeptUtils remoteDeptUtils;

	@Override
	public Page getBasicPlaceFloorPage(Page page, BasicPlaceFloorDTO basicPlaceFloorDTO) {
		//多表查询
		MPJLambdaWrapper<BasicPlaceFloorEntity> wrapper = createBaseWrapper();
		if (basicPlaceFloorDTO.getFloorId()!=null){
			wrapper.eq(BasicPlaceFloorEntity::getBuildingId, basicPlaceFloorDTO.getBuildingId());
		}

		if (StrUtil.isNotEmpty(basicPlaceFloorDTO.getFloorName())) {
			wrapper.like(BasicPlaceFloorEntity::getFloorName, basicPlaceFloorDTO.getFloorName());
		}

		if (ObjectUtil.isNotEmpty(basicPlaceFloorDTO.getFloorId())) {
			wrapper.in(BasicPlaceFloorEntity::getId, basicPlaceFloorDTO.getFloorId());
		}
		wrapper.eq(BasicPlaceFloorEntity::getBuildingId, basicPlaceFloorDTO.getBuildingId());

		Page basicPlaceFloorPage = this.getBaseMapper().selectJoinPage(page, BasicPlaceFloorVO.class, wrapper);

		List<BasicPlaceFloorVO> records = basicPlaceFloorPage.getRecords();
		BasicPlaceFloorEntity basicPlaceFloorEntity = new BasicPlaceFloorEntity();
		records.stream().forEach(record -> {
			Long floorId = record.getId();
			int installCount = (int) basicInstallLocationService.count(new QueryWrapper<BasicInstallLocationEntity>()
					.eq("floor_id", floorId));
			record.setInstallCount(installCount);
			basicPlaceFloorEntity.setId(record.getId());
			basicPlaceFloorEntity.setInstallCount(installCount);
			baseMapper.updateById(basicPlaceFloorEntity);
		});

		return basicPlaceFloorPage;
	}

	@Override
	public List<BasicPlaceFloorEntity> getBasicPlaceFloorList(BasicPlaceFloorDTO basicPlaceFloorDTO) {
		LambdaQueryWrapper<BasicPlaceFloorEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(BasicPlaceFloorEntity::getBuildingId, basicPlaceFloorDTO.getBuildingId());
		List<BasicPlaceFloorEntity> basicPlaceFloorList = this.getBaseMapper().selectList(wrapper);
		return basicPlaceFloorList;
	}


	@Override
	public BasicPlaceFloorVO getById(Long id) {
		//多表查询
		MPJLambdaWrapper<BasicPlaceFloorEntity> wrapper = createBaseWrapper().eq(BasicPlaceFloorEntity::getId, id);

		BasicPlaceFloorVO basicPlaceFloorVO = this.getBaseMapper().selectJoinOne(BasicPlaceFloorVO.class, wrapper);

		Long deptId = basicPlaceFloorVO.getDeptId();
		if (deptId != null) {
			String deptName = remoteDeptUtils.getDeptName(deptId);
			basicPlaceFloorVO.setDeptName(deptName);
		}

		return basicPlaceFloorVO;
	}

	private MPJLambdaWrapper<BasicPlaceFloorEntity> createBaseWrapper() {
		return new MPJLambdaWrapper<BasicPlaceFloorEntity>()
				.selectAll(BasicPlaceFloorEntity.class)
				.selectAs(BasicPlaceBuildingEntity::getBuildingName, BasicPlaceFloorVO::getBuildingName)
				.selectAs(BasicPlaceBuildingEntity::getDeptId, BasicPlaceFloorVO::getDeptId)

				.leftJoin(BasicPlaceBuildingEntity.class, BasicPlaceBuildingEntity::getId, BasicPlaceFloorEntity::getBuildingId);

	}
}